package com.xudow.app.dynamicstate_old.module.discovery;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.jude.beam.bijection.RequiresPresenter;
import com.jude.beam.expansion.list.BeamListFragment;
import com.jude.beam.expansion.list.ListConfig;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.utils.JUtils;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.ImageModel;
import com.xudow.app.dynamicstate_old.data.server.ErrorTransform;
import com.xudow.app.dynamicstate_old.domain.entity.AdvInfo;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;
import com.xudow.app.dynamicstate_old.module.discovery.viewholder.DynamicContentViewHolder;
import com.xudow.app.dynamicstate_old.module.discovery.viewholder.PostLongViewHolder;
import com.xudow.app.dynamicstate_old.module.discovery.viewholder.PostNormalViewHolder;
import com.xudow.app.dynamicstate_old.module.discovery.viewholder.RepostViewHolder;
import com.xudow.app.dynamicstate_old.module.recommend.RecommendFragment;
import com.xudow.app.ui.ActivityViewActivity;
import com.xudow.app.ui.CourseDetailActivity;
import com.xudow.app.ui.widget.gallery.XViewPaper;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by troyl on 2016/4/29.
 */
@RequiresPresenter(DiscoverPresenter.class)
public class DiscoverFragment extends BeamListFragment<DiscoverPresenter, Dynamic> {
    private static final int POST_NORMAL = 1;
    private static final int POST_LONG = 2;
    private static final int REPOST = 3;

    @Override
    public ListConfig getConfig() {
        return super.getConfig()
                .setContainerEmptyAble(false);
    }

    @Override
    public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
        DynamicContentViewHolder viewHolder = null;
        if (viewType == POST_LONG) {
            viewHolder = new PostLongViewHolder(getPresenter().getAdapter(), parent);
        } else if (viewType == POST_NORMAL) {
            viewHolder = new PostNormalViewHolder(getPresenter().getAdapter(), parent);
        } else if (viewType == REPOST) {
            viewHolder = new RepostViewHolder(getPresenter().getAdapter(), parent);
        } else {
            viewHolder = new PostNormalViewHolder(getPresenter().getAdapter(), parent);
        }
        return viewHolder;
    }

    RollPagerView banner;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getListView().addItemDecoration(new DividerDecoration(getResources().getColor(R.color.gray100), JUtils.dip2px(12)));
        getPresenter().getAdapter().addHeader(new RecyclerArrayAdapter.ItemView() {
            @Override
            public View onCreateView(ViewGroup parent) {
                View view = LayoutInflater.from(getContext()).inflate(R.layout.header_dicover, parent, false);
                banner = (RollPagerView) view.findViewById(R.id.banner);
                ViewGroup.LayoutParams lp = banner.getLayoutParams();
                lp.height = (int) (JUtils.getScreenWidth() * 0.4);
                banner.setLayoutParams(lp);
                initBanner();
                return view;
            }

            @Override
            public void onBindView(View headerView) {

            }
        });
    }

    private void initBanner() {
        DynamicModel.getInstance().getBanner(4)
                .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
                .subscribe(advInfos -> {
                    if (advInfos != null && advInfos.size() != 0) {
                        banner.setAdapter(new StaticPagerAdapter() {

                            @Override
                            public int getCount() {
                                return advInfos.size();
                            }

                            @Override
                            public View getView(ViewGroup container, int position) {
                                ImageView view = new ImageView(container.getContext());
                                view.setScaleType(ImageView.ScaleType.CENTER_CROP);
                                view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                                Glide.with(getActivity()).load(ImageModel.getXueDouImage(advInfos.get(position).getImgPath())).into(view);
                                return view;
                            }

                            @Override
                            public Object instantiateItem(ViewGroup container, int position) {

                                View v = container.getChildAt(position);
                                if (v == null) {
                                    v = getView(container, position);
                                    container.addView(v);
                                }
                                onBind(v, position);
                                v.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        AdvInfo advInfo = advInfos.get(position);
                                        Intent intent = null;
                                        String str = "";
                                        switch (advInfo.getType()) {
                                            case 1:// 活动
                                                Long activityId = advInfo.getRefId();
                                                if (null != activityId) {
                                                    intent = new Intent(getContext(), ActivityViewActivity.class);
                                                    intent.putExtra("activityId", activityId);
                                                }
                                                break;
                                            case 2://课程
                                                Long courseId = advInfo.getRefId();
                                                if (null != courseId) {
                                                    intent = new Intent(getContext(), CourseDetailActivity.class);
                                                    intent.putExtra("courseId", courseId);
                                                }
                                                break;
                                            case 3://外部链接
                                                intent = new Intent(getContext(), ActivityViewActivity.class);
                                                intent.putExtra("pageurl", advInfo.getPageUrl());
                                                break;
                                            case 4://自编辑页面
                                                intent = new Intent(getContext(), ActivityViewActivity.class);
                                                intent.putExtra("pagecontent", advInfo.getPageContent());
                                                break;

                                        }
                                        if (null != intent)
                                            getActivity().startActivity(intent);

                                    }
                                });
                                return v;
                            }

                        });
                    } else {
                        banner.setVisibility(View.GONE);
                    }
                });
    }

    @Override
    public int getViewType(int position) {
        Dynamic dynamic = getPresenter().getAdapter().getItem(position);
        // 1：长文，2：普通
        int type = dynamic.getType();
        // 上级动态主键, 为0说明，这条动态不是转发的
        long pId = dynamic.getpId();
        if (pId != 0)
            return REPOST;
        else if (type == 1)
            return POST_LONG;
        else if (type == 2)
            return POST_NORMAL;
        return 0;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            getPresenter().onRefresh();
        }
    }


}